package com.cn.action;

import com.cn.Intface.PhoneYz;
import com.cn.entity.UserEntity;
import com.cn.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class RegisterAction implements ActionInterface {
    UserService us = new UserService();

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String uphone = request.getParameter("phone");
        String upassword = request.getParameter("password");
        UserEntity userEntity = new UserEntity();
        userEntity.setPassword(upassword);
        userEntity.setPhone(uphone);
        UserService us = new UserService();
        UserEntity userEntity1 = new UserEntity();
        String smscode = request.getParameter("smscode");
        PhoneYz phoneYz = (PhoneYz) request.getSession().getAttribute("PhoneYz");
        request.removeAttribute("lgqr");
        request.removeAttribute("lgzccg");
        request.removeAttribute("lgqre");
        if (smscode.equals(phoneYz.getUserRandom())) {
            userEntity1 = us.queryOne(userEntity, "zc");
            if (userEntity1 != null) {
                request.setAttribute("lgqr", "注册的手机号码已存在");
                return "login.jsp";
            } else {
                us.addUser(userEntity);
                request.getSession().setAttribute("userent", userEntity);
                request.setAttribute("lgzccg", "注册成功");
                return "login.jsp";
            }
        } else {
            request.setAttribute("lgqre", "验证码输入错误！！！");
            return "login.jsp";
        }

    }
}
